
package DataFormatConversionComponents;


/**
 * This class is used to simplify data conversion from one base to another
 * 
 *
 */
public class NumberConverter {
	
	/**
	 * this method converts a binary number to decimal
	 * @param binaryInput Array if integers that has elements with value = 0,1.
	 * @return returns the calculated conversion as an integer
	 */
	public int binaryToDecimal(int[] binaryInput){
		int result = 0;
		//integer "i" indexes binaryInput from right to left (lsb first) && integer "j" 
		//controls "weight" of each bit
		for(int i = binaryInput.length - 1, j = 0; i >= 0; i--, j++)
			result += binaryInput[i] * Math.pow( 2, j );
		
	
		return result;
	}
	

	/**
	 * This method converts an integer into its binary equivalent. The binary number
	 * is represented with an array of integers, where each element represents one bit
	 * @param intInput string of size 4
	 * @return array of integers, with length = 16, that represents the converted binary equivalent 
	 */
	public int[] hexToBinary(String hexInput){
		
		/*Assume hexInput= AFC1
		 * the method will tokenize
		 * char first = A;
		 * char second = F;
		 * char third = C;
		 * char fourt = 1;
		 *
		 *and returns an array of lenght 16 with the binary code
		 *for example
		 *   1010 1111 1100 0001
		 *    A     F   C    1
		 * 
		 */
		
	
		String currentChar;
		int arraySize= hexInput.length()*4;
		int[] binaryCodeArray = new int[arraySize];
		
		int binaryArrayIndex;
		
	
		//for( int tt = 0; tt < 4; tt++ )
			//System.out.println( "HEXINPUT " + hexInput );

		
      for(int i = 0 ; i<hexInput.length(); i++){
    	  
    	    binaryArrayIndex = arraySize-1;
			binaryArrayIndex= binaryArrayIndex-(i*4);
			
			//System.out.println( i );
			currentChar = hexInput.substring(i, i+1);
			
			if(currentChar.equalsIgnoreCase("A")){
			    binaryCodeArray[binaryArrayIndex] = 1;
			    binaryCodeArray[binaryArrayIndex-1] = 0;
			    binaryCodeArray[binaryArrayIndex-2] = 1;
			    binaryCodeArray[binaryArrayIndex-3] = 0;
			}
			else if(currentChar.equalsIgnoreCase("B")){
			    binaryCodeArray[binaryArrayIndex] = 1;
			    binaryCodeArray[binaryArrayIndex-1] = 0;
			    binaryCodeArray[binaryArrayIndex-2] = 1;
			    binaryCodeArray[binaryArrayIndex-3] = 1;
			}
			else if(currentChar.equalsIgnoreCase("C")){
			    binaryCodeArray[binaryArrayIndex] = 1;
			    binaryCodeArray[binaryArrayIndex-1] = 1;
			    binaryCodeArray[binaryArrayIndex-2] = 0;
			    binaryCodeArray[binaryArrayIndex-3] = 0;
			}
			else if(currentChar.equalsIgnoreCase("D")){
			    binaryCodeArray[binaryArrayIndex] = 1;
			    binaryCodeArray[binaryArrayIndex-1] = 1;
			    binaryCodeArray[binaryArrayIndex-2] = 0;
			    binaryCodeArray[binaryArrayIndex-3] = 1;
			}
			else if(currentChar.equalsIgnoreCase("E")){
			    binaryCodeArray[binaryArrayIndex] = 1;
			    binaryCodeArray[binaryArrayIndex-1] = 1;
			    binaryCodeArray[binaryArrayIndex-2] = 1;
			    binaryCodeArray[binaryArrayIndex-3] = 0;
			}
			else if(currentChar.equalsIgnoreCase("F")){
			    binaryCodeArray[binaryArrayIndex] = 1;
			    binaryCodeArray[binaryArrayIndex-1] = 1;
			    binaryCodeArray[binaryArrayIndex-2] = 1;
			    binaryCodeArray[binaryArrayIndex-3] = 1;
			}
			else if(currentChar.equalsIgnoreCase("1")){
			    binaryCodeArray[binaryArrayIndex] = 0;
			    binaryCodeArray[binaryArrayIndex-1] = 0;
			    binaryCodeArray[binaryArrayIndex-2] = 0;
			    binaryCodeArray[binaryArrayIndex-3] = 1;
			}
			else if(currentChar.equalsIgnoreCase("2")){
			    binaryCodeArray[binaryArrayIndex] = 0;
			    binaryCodeArray[binaryArrayIndex-1] = 0;
			    binaryCodeArray[binaryArrayIndex-2] = 1;
			    binaryCodeArray[binaryArrayIndex-3] = 0;
			}
			else if(currentChar.equalsIgnoreCase("3")){
			    binaryCodeArray[binaryArrayIndex] = 0;
			    binaryCodeArray[binaryArrayIndex-1] = 0;
			    binaryCodeArray[binaryArrayIndex-2] = 1;
			    binaryCodeArray[binaryArrayIndex-3] = 1;
			}
			else if(currentChar.equalsIgnoreCase("4")){
			    binaryCodeArray[binaryArrayIndex] = 0;
			    binaryCodeArray[binaryArrayIndex-1] =1;
			    binaryCodeArray[binaryArrayIndex-2] = 0;
			    binaryCodeArray[binaryArrayIndex-3] = 0;
			}
			else if(currentChar.equalsIgnoreCase("5")){
			    binaryCodeArray[binaryArrayIndex] = 0;
			    binaryCodeArray[binaryArrayIndex-1] =1;
			    binaryCodeArray[binaryArrayIndex-2] = 0;
			    binaryCodeArray[binaryArrayIndex-3] = 1;
			}
			else if(currentChar.equalsIgnoreCase("6")){
			    binaryCodeArray[binaryArrayIndex] = 0;
			    binaryCodeArray[binaryArrayIndex-1] =1;
			    binaryCodeArray[binaryArrayIndex-2] = 1;
			    binaryCodeArray[binaryArrayIndex-3] = 0;
			}
			else if(currentChar.equalsIgnoreCase("7")){
			    binaryCodeArray[binaryArrayIndex] = 0;
			    binaryCodeArray[binaryArrayIndex-1] =1;
			    binaryCodeArray[binaryArrayIndex-2] = 1;
			    binaryCodeArray[binaryArrayIndex-3] = 1;
			}
			else if(currentChar.equalsIgnoreCase("8")){
			    binaryCodeArray[binaryArrayIndex] = 1;
			    binaryCodeArray[binaryArrayIndex-1] =0;
			    binaryCodeArray[binaryArrayIndex-2] = 0;
			    binaryCodeArray[binaryArrayIndex-3] = 0;
			}
			else if(currentChar.equalsIgnoreCase("9")){
			    binaryCodeArray[binaryArrayIndex] = 1;
			    binaryCodeArray[binaryArrayIndex-1] =0;
			    binaryCodeArray[binaryArrayIndex-2] = 0;
			    binaryCodeArray[binaryArrayIndex-3] = 1;
			}
			
			
		}
		
	return binaryCodeArray;
	}
	
	/**
	 * This method uses the previously implemented methods hexToBinary and binaryToInt 
	 * to convert a hex number stored as a string to a decimal
	 * @param hexInput
	 * @return
	 */
	public int hexToDecimal(String hexInput){
		return this.binaryToDecimal( hexToBinary(hexInput) );
	}
	

	/**
	 * This method is used to convert a string input of length = 1 to its corresponding ASCII code
	 * @param input string of length = 1
	 * @return integer ASCII code 
	 */
	public int stringToASCII(String input){
		    char c = input.charAt(0);
	        int asciiCode = (int) c;
	        
	        return asciiCode;
	       
	        }
	
	/**
	 * This method is used to convert an integer input to its corresponding binary equivalent
	 * @param input integer to perform base change to binary
	 * @return array of integers with binary conversion
	 */
	public int[] integerToBinary(int input){
	    String str = Integer.toBinaryString(input); //needs to be stored in an array
        int arr[] = new int[str.length()];
        for( int x =0 ; x<str.length(); x++){
        	arr[x]=
        		Integer.parseInt(str.substring(x, x+1));
        	}
    
        return arr;
	}
	
	/**
	 * This algorithm converts an integer array (binary bits) to its hexadecimal equivalent
	 * @param binaryArr array to convert to hexadecimal
	 * @return string containing hexadecimal equivalent of binaryArr
	 */
	public String binaryToHex(int[] binaryArr){

		String str =new String(); //String to return with Hex code

		//maps position 0 1 2 3 of the array to a Hex numbers in str position 0 and so on.
		for(int i = 0 ; i<binaryArr.length; i=i+4){

			String codeToConvert= Integer.toString(binaryArr[i])
			+Integer.toString(binaryArr[i+1])
			+Integer.toString(binaryArr[i+2])
			+Integer.toString(binaryArr[i+3]);

			if(codeToConvert.equalsIgnoreCase("0000"))
				str= str + "0";
			else if(codeToConvert.equalsIgnoreCase("0001"))
				str= str + "1";
			else if(codeToConvert.equalsIgnoreCase("0010"))
				str= str + "2";
			else if(codeToConvert.equalsIgnoreCase("0011"))
				str= str + "3";
			else if(codeToConvert.equalsIgnoreCase("0100"))
				str= str + "4";
			else if(codeToConvert.equalsIgnoreCase("0101"))
				str= str + "5";
			else if(codeToConvert.equalsIgnoreCase("0110"))
				str= str + "6";
			else if(codeToConvert.equalsIgnoreCase("0111"))
				str= str + "7";
			else if(codeToConvert.equalsIgnoreCase("1000"))
				str= str + "8";
			else if(codeToConvert.equalsIgnoreCase("1001"))
				str= str + "9";
			else if(codeToConvert.equalsIgnoreCase("1010"))
				str= str + "A";
			else if(codeToConvert.equalsIgnoreCase("1011"))
				str= str + "B";
			else if(codeToConvert.equalsIgnoreCase("1100"))
				str= str + "C";
			else if(codeToConvert.equalsIgnoreCase("1101"))
				str= str + "D";
			else if(codeToConvert.equalsIgnoreCase("1110"))
				str= str + "E";
			else if(codeToConvert.equalsIgnoreCase("1111"))
				str= str + "F";
			
		}
		
		return str;
		
	}
	

	/**
	 * casting an integer ASCII code back to its String equivalent
	 * @param asciiCode integer ASCII code to convert
	 * @return String containing converted ASCII code
	 */
	public String ASCIIToChar(int asciiCode){
		return (char) asciiCode+"";
	}
	
	
}
